home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / WFC010.ZIP / SRC / CEVNTLOG.CPP < prev    next >
C/C++ Source or Header  |  1995-12-07  |  16KB  |  640 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like as long as you don't try to sell it.
  10. **
  11. ** Any attempt to sell WFC in source code form must have the permission
  12. ** of the original author. You can produce commercial executables with
  13. ** WFC but you can't sell WFC.
  14. **
  15. ** Copyright, 1995, Samuel R. Blackburn
  16. **
  17. ** $Workfile: $
  18. ** $Revision: $
  19. ** $Modtime: $
  20. */
  21.  
  22. #if defined( _DEBUG )
  23. #undef THIS_FILE
  24. static char BASED_CODE THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. IMPLEMENT_DYNAMIC( CEventLog, CObject );
  28.  
  29. #if defined( _DEBUG )
  30. #define new DEBUG_NEW
  31. #endif
  32.  
  33. CEventLog::CEventLog()
  34. {
  35.    m_Initialize();
  36. }
  37.  
  38. CEventLog::CEventLog( LPCTSTR source_name )
  39. {
  40.    m_Initialize();
  41.    RegisterSource( source_name );
  42. }
  43.  
  44. CEventLog::~CEventLog()
  45. {
  46.    if ( m_EventSourceHandle != INVALID_HANDLE_VALUE )
  47.    {
  48.       DeregisterSource();
  49.    }
  50.  
  51.    if ( m_LogHandle != INVALID_HANDLE_VALUE )
  52.    {
  53.       Close();
  54.    }
  55.  
  56.    m_Initialize();
  57. }
  58.  
  59. void CEventLog::m_Initialize( void )
  60. {
  61.    ASSERT_VALID( this );
  62.  
  63.    ComputerName.Empty();
  64.    LogName.Empty();
  65.  
  66.    m_LogHandle                 = INVALID_HANDLE_VALUE;
  67.    m_EventSourceHandle         = INVALID_HANDLE_VALUE;
  68.    m_ErrorCode                 = 0;
  69.    m_NumberOfBytesRead         = 0;
  70.    m_NumberOfBytesInNextRecord = 0;
  71. }
  72.  
  73. BOOL CEventLog::Backup( LPCTSTR name_of_backup_file )
  74. {
  75.    ASSERT_VALID( this );
  76.    ASSERT( name_of_backup_file != NULL );
  77.  
  78.    if ( name_of_backup_file == NULL )
  79.    {
  80.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  81.       return( FALSE );
  82.    }
  83.  
  84.    BOOL return_value = ::BackupEventLog( m_LogHandle, name_of_backup_file );
  85.  
  86.    if ( return_value != TRUE )
  87.    {
  88.       m_ErrorCode = ::GetLastError();
  89.    }
  90.  
  91.    return( return_value );
  92. }
  93.  
  94. BOOL CEventLog::Clear( LPCTSTR name_of_backup_file )
  95. {
  96.    ASSERT_VALID( this );
  97.  
  98.    /*
  99.    ** name_of_backup_file can be NULL
  100.    */
  101.  
  102.    BOOL return_value = ::ClearEventLog( m_LogHandle, name_of_backup_file );
  103.  
  104.    if ( return_value != TRUE )
  105.    {
  106.       m_ErrorCode = ::GetLastError();
  107.    }
  108.  
  109.    return( return_value );
  110. }
  111.  
  112. BOOL CEventLog::Close( void )
  113. {
  114.    ASSERT_VALID( this );
  115.  
  116.    BOOL return_value = ::CloseEventLog( m_LogHandle );
  117.  
  118.    if ( return_value != TRUE )
  119.    {
  120.       m_ErrorCode = ::GetLastError();
  121.    }
  122.  
  123.    m_LogHandle = INVALID_HANDLE_VALUE;
  124.  
  125.    return( return_value );
  126. }
  127.  
  128. BOOL CEventLog::CreateApplicationLog( LPCTSTR application_name, LPCTSTR message_resource_file, DWORD supported_types )
  129. {
  130.    ASSERT_VALID( this );
  131.    ASSERT( application_name      != NULL );
  132.    ASSERT( message_resource_file != NULL );
  133.  
  134.    if ( application_name == NULL || message_resource_file == NULL )
  135.    {
  136.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  137.       return( FALSE );
  138.    }
  139.  
  140.    if ( application_name[ 0 ] == 0x00 || message_resource_file[ 0 ] == 0x00 )
  141.    {
  142.       return( FALSE );
  143.    }
  144.  
  145.    CRegistry registry;
  146.  
  147.    if ( registry.Connect( CRegistry::keyLocalMachine ) != TRUE )
  148.    {
  149.       m_ErrorCode = registry.GetErrorCode();
  150.       return( FALSE );
  151.    }
  152.  
  153.    CString log_key_name( "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\" );
  154.  
  155.    log_key_name += application_name;
  156.  
  157.    if ( registry.Create( log_key_name ) != TRUE )
  158.    {
  159.       m_ErrorCode = registry.GetErrorCode();
  160.       return( FALSE );
  161.    }
  162.  
  163.    if ( registry.SetValue( "EventMessageFile", CRegistry::typeUnexpandedString, (LPBYTE) message_resource_file, strlen( message_resource_file ) + 1 ) != TRUE )
  164.    {
  165.       m_ErrorCode = registry.GetErrorCode();
  166.       return( FALSE );
  167.    }
  168.  
  169.    if ( registry.SetValue( "TypesSupported", supported_types ) != TRUE )
  170.    {
  171.       m_ErrorCode = registry.GetErrorCode();
  172.       return( FALSE );
  173.    }
  174.  
  175.    return( TRUE );
  176. }
  177.  
  178. BOOL CEventLog::DeleteApplicationLog( LPCTSTR application_name )
  179. {
  180.    ASSERT_VALID( this );
  181.    ASSERT( application_name != NULL );
  182.  
  183.    if ( application_name == NULL )
  184.    {
  185.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  186.       return( FALSE );
  187.    }
  188.  
  189.    if ( application_name[ 0 ] == 0x00 )
  190.    {
  191.       return( FALSE );
  192.    }
  193.  
  194.    CRegistry registry;
  195.  
  196.    if ( registry.Connect( CRegistry::keyLocalMachine ) != TRUE )
  197.    {
  198.       m_ErrorCode = registry.GetErrorCode();
  199.       return( FALSE );
  200.    }
  201.  
  202.    CString log_key_name( "SYSTEM\\CurrentControl\\Services\\EventLog\\Application\\" );
  203.  
  204.    log_key_name += application_name;
  205.  
  206.    if ( registry.DeleteKey( log_key_name ) != TRUE )
  207.    {
  208.       m_ErrorCode = registry.GetErrorCode();
  209.       return( FALSE );
  210.    }
  211.  
  212.    /*
  213.    ** Microsoft has a bug in this area. Even though we deleted the application from the
  214.    ** HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\
  215.    ** registry area, they don't provide a way to delete the application from the 
  216.    ** HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\Sources
  217.    ** value. The application name is one of the strings in this REG_MULTI_SZ value. We
  218.    ** still need to delete it from there. The names listed in this value appear in the 
  219.    ** "Source" combobox of the Event Viewer application View->Filter Events... menu selection.
  220.    */
  221.  
  222.    if ( registry.Open( "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application" ) == TRUE )
  223.    {
  224.       CStringArray sources;
  225.  
  226.       if ( registry.GetValue( "Sources", sources ) == TRUE )
  227.       {
  228.          int index = 0;
  229.          int number_of_sources = sources.GetSize();
  230.  
  231.          BOOL application_was_found = FALSE;
  232.  
  233.          while( index < number_of_sources )
  234.          {
  235.             if ( sources[ index ] == application_name )
  236.             {
  237.                application_was_found = TRUE;
  238.                sources.RemoveAt( index );
  239.                index = number_of_sources;
  240.             }
  241.  
  242.             index++;
  243.          }
  244.  
  245.          if ( application_was_found == TRUE )
  246.          {
  247.             registry.SetValue( "Sources", sources );
  248.          }
  249.       }
  250.    }
  251.  
  252.    return( TRUE );
  253. }
  254.  
  255. BOOL CEventLog::DeregisterSource( void )
  256. {
  257.    ASSERT_VALID( this );
  258.  
  259.    BOOL return_value = TRUE;
  260.  
  261.    if ( m_EventSourceHandle != INVALID_HANDLE_VALUE )
  262.    {
  263.       return_value = ::DeregisterEventSource( m_EventSourceHandle );
  264.  
  265.       if ( return_value != TRUE )
  266.       {
  267.          m_ErrorCode = ::GetLastError();
  268.       }
  269.  
  270.       m_EventSourceHandle = INVALID_HANDLE_VALUE;
  271.    }
  272.  
  273.    return( return_value );
  274. }
  275.  
  276. #if defined( _DEBUG )
  277.  
  278. void CEventLog::Dump( CDumpContext& dump_context ) const
  279. {
  280.    CObject::Dump( dump_context );
  281.  
  282.    dump_context << "m_LogHandle = "                 << m_LogHandle                 << "\n";
  283.    dump_context << "m_EventSourceHandle = "         << m_EventSourceHandle         << "\n";
  284.    dump_context << "m_ErrorCode = "                 << m_ErrorCode                 << "\n";
  285.    dump_context << "m_NumberOfBytesRead = "         << m_NumberOfBytesRead         << "\n";
  286.    dump_context << "m_NumberOfBytesInNextRecord = " << m_NumberOfBytesInNextRecord << "\n";
  287.    dump_context << "ComputerName = \""              << ComputerName                << "\"\n";
  288.    dump_context << "LogName = \""                   << LogName                     << "\"\n";
  289. }
  290.  
  291. #endif // _DEBUG
  292.  
  293. DWORD CEventLog::GetErrorCode( void ) const
  294. {
  295.    ASSERT_VALID( this );
  296.    return( m_ErrorCode );
  297. }
  298.  
  299. BOOL CEventLog::GetNumberOfRecords( DWORD& number_of_records )
  300. {
  301.    ASSERT_VALID( this );
  302.  
  303.    BOOL return_value = ::GetNumberOfEventLogRecords( m_LogHandle, &number_of_records );
  304.  
  305.    if ( return_value != TRUE )
  306.    {
  307.       m_ErrorCode = ::GetLastError();
  308.    }
  309.  
  310.    return( return_value );
  311. }
  312.  
  313. BOOL CEventLog::NotifyChange( HANDLE event_handle, HANDLE user_log_handle )
  314. {
  315.    ASSERT_VALID( this );
  316.    ASSERT( event_handle != INVALID_HANDLE_VALUE );
  317.  
  318.    if ( event_handle == INVALID_HANDLE_VALUE )
  319.    {
  320.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  321.       return( FALSE );
  322.    }
  323.  
  324.    if ( user_log_handle == INVALID_HANDLE_VALUE || user_log_handle == NULL )
  325.    {
  326.       user_log_handle = m_LogHandle;
  327.    }
  328.  
  329.    BOOL return_value = ::NotifyChangeEventLog( user_log_handle, event_handle );
  330.  
  331.    if ( return_value != TRUE )
  332.    {
  333.       m_ErrorCode = ::GetLastError();
  334.    }
  335.  
  336.    return( return_value );
  337. }
  338.  
  339. BOOL CEventLog::Open( LPCTSTR log_name, LPCTSTR name_of_computer )
  340. {
  341.    ASSERT_VALID( this );
  342.    ASSERT( log_name != NULL );
  343.  
  344.    /*
  345.    ** name_of_computer can be NULL
  346.    */
  347.                  
  348.    if ( log_name == NULL )
  349.    {
  350.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  351.       return( FALSE );
  352.    }
  353.  
  354.    BOOL return_value = TRUE;
  355.  
  356.    m_LogHandle = ::OpenEventLog( name_of_computer, log_name );
  357.  
  358.    if ( m_LogHandle == NULL )
  359.    {
  360.       m_LogHandle  = INVALID_HANDLE_VALUE;
  361.       m_ErrorCode  = ::GetLastError();
  362.       return_value = FALSE;
  363.    }
  364.    else
  365.    {
  366.       if ( name_of_computer == NULL )
  367.       {
  368.          TCHAR computer_name[ MAX_PATH ] = "";
  369.          DWORD size = sizeof( computer_name );
  370.  
  371.          if ( ::GetComputerName( computer_name, &size ) == TRUE )
  372.          {
  373.             ComputerName = computer_name;
  374.          }
  375.          else
  376.          {
  377.             ComputerName.Empty();
  378.          }
  379.       }
  380.       else
  381.       {
  382.          ComputerName = name_of_computer;
  383.       }
  384.    }
  385.  
  386.    return( return_value );
  387. }
  388.  
  389. BOOL CEventLog::OpenBackup( LPCTSTR name_of_backup_file, LPCTSTR name_of_computer )
  390. {
  391.    ASSERT_VALID( this );
  392.    ASSERT( name_of_backup_file != NULL );
  393.  
  394.    /*
  395.    ** name_of_computer can be NULL
  396.    */
  397.  
  398.    if ( name_of_backup_file == NULL )
  399.    {
  400.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  401.       return( FALSE );
  402.    }
  403.  
  404.    BOOL return_value = TRUE;
  405.  
  406.    m_LogHandle = ::OpenBackupEventLog( name_of_computer, name_of_backup_file );
  407.  
  408.    if ( m_LogHandle == NULL )
  409.    {
  410.       m_LogHandle = INVALID_HANDLE_VALUE;
  411.       m_ErrorCode = ::GetLastError();
  412.       return_value = FALSE;
  413.    }
  414.    else
  415.    {
  416.       if ( name_of_computer == NULL )
  417.       {
  418.          TCHAR computer_name[ MAX_PATH ] = "";
  419.          DWORD size = sizeof( computer_name );
  420.  
  421.          if ( ::GetComputerName( computer_name, &size ) == TRUE )
  422.          {
  423.             ComputerName = computer_name;
  424.          }
  425.          else
  426.          {
  427.             ComputerName.Empty();
  428.          }
  429.       }
  430.       else
  431.       {
  432.          ComputerName = name_of_computer;
  433.       }
  434.    }
  435.  
  436.    return( return_value );
  437. }
  438.  
  439. BOOL CEventLog::Read( DWORD record_number, LPVOID buffer, DWORD& number_of_bytes_to_read, DWORD how_to_read )
  440. {
  441.    ASSERT_VALID( this );
  442.    ASSERT( buffer != NULL );
  443.  
  444.    if ( buffer == NULL )
  445.    {
  446.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  447.       return( FALSE );
  448.    }
  449.  
  450.    BOOL return_value = ::ReadEventLog( m_LogHandle,
  451.                                        how_to_read,
  452.                                        record_number,
  453.                                        buffer,
  454.                                        number_of_bytes_to_read,
  455.                                       &m_NumberOfBytesRead,
  456.                                       &m_NumberOfBytesInNextRecord );
  457.  
  458.    if ( return_value != TRUE )
  459.    {
  460.       m_ErrorCode = ::GetLastError();
  461.    }
  462.  
  463.    return( return_value );
  464. }
  465.  
  466. BOOL CEventLog::RegisterSource( LPCTSTR source_name, LPCTSTR name_of_computer )
  467. {
  468.    ASSERT_VALID( this );
  469.    ASSERT( source_name != NULL );
  470.  
  471.    /*
  472.    ** name_of_computer can be NULL
  473.    */
  474.  
  475.    if ( source_name == NULL )
  476.    {
  477.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  478.       return( FALSE );
  479.    }
  480.  
  481.    BOOL return_value = TRUE;
  482.  
  483.    if ( m_EventSourceHandle != INVALID_HANDLE_VALUE )
  484.    {
  485.       DeregisterSource();
  486.    }
  487.  
  488.    m_EventSourceHandle = ::RegisterEventSource( name_of_computer, source_name );
  489.  
  490.    if ( m_EventSourceHandle == NULL )
  491.    {
  492.       TRACE( "RegisterEventSource returned NULL\n" );
  493.       m_EventSourceHandle = INVALID_HANDLE_VALUE;
  494.       m_ErrorCode          = ::GetLastError();
  495.       return_value        = FALSE;
  496.    }
  497.  
  498.    return( return_value );
  499. }
  500.  
  501. BOOL CEventLog::Report( EventType event_type,
  502.                         WORD      category,
  503.                         DWORD     event_identifier,
  504.                         WORD      number_of_strings,
  505.                         LPCTSTR * string_array,
  506.                         DWORD     number_of_raw_data_bytes,
  507.                         LPVOID    raw_data_buffer,
  508.                         PSID      user_security_identifier )
  509. {
  510.    ASSERT_VALID( this );
  511.  
  512.    BYTE security_identifier_buffer[ 4096 ];
  513.  
  514.    DWORD size_of_security_identifier_buffer = sizeof( security_identifier_buffer );
  515.  
  516.    if ( user_security_identifier == NULL )
  517.    {
  518.       TCHAR user_name[ 256 ];
  519.       DWORD size_of_user_name  = sizeof( user_name );
  520.  
  521.       TCHAR domain_name[ 256 ];
  522.       DWORD size_of_domain_name = sizeof( domain_name );
  523.  
  524.       SID_NAME_USE type_of_security_identifier;
  525.  
  526.       ::ZeroMemory( user_name, size_of_user_name );
  527.       ::ZeroMemory( domain_name, size_of_domain_name );
  528.       ::ZeroMemory( security_identifier_buffer, size_of_security_identifier_buffer );
  529.  
  530.       ::GetUserName( user_name, &size_of_user_name );
  531.  
  532.       if ( ::LookupAccountName( NULL,
  533.                                 user_name,
  534.                                &security_identifier_buffer,
  535.                                &size_of_security_identifier_buffer,
  536.                                 domain_name,
  537.                                &size_of_domain_name,
  538.                                &type_of_security_identifier ) == TRUE )
  539.       {
  540.          user_security_identifier = security_identifier_buffer;
  541.       }
  542.    }
  543.  
  544.    BOOL return_value = FALSE;
  545.  
  546.    if ( m_EventSourceHandle != INVALID_HANDLE_VALUE )
  547.    {
  548.       return_value = ::ReportEvent( m_EventSourceHandle,
  549.                                     event_type,
  550.                                     category,
  551.                                     event_identifier,
  552.                                     user_security_identifier,
  553.                                     number_of_strings,
  554.                                     number_of_raw_data_bytes,
  555.                                     string_array,
  556.                                     raw_data_buffer );
  557.  
  558.       TRACE( "CEventLog::Report(), Calling ReportEvent() went OK\n" );
  559.  
  560.       if ( return_value != TRUE )
  561.       {
  562.          m_ErrorCode = ::GetLastError();
  563.       }
  564.    }
  565.    else
  566.    {
  567.       m_ErrorCode = ERROR_INVALID_HANDLE;
  568.    }
  569.  
  570.    return( return_value );
  571. }
  572.  
  573. BOOL CEventLog::Report( LPCTSTR log_name, DWORD message_string_resource_identifier, WORD number_of_strings, LPCTSTR* string_array )
  574. {
  575.    ASSERT_VALID( this );
  576.    ASSERT( log_name != NULL );
  577.  
  578.    if ( log_name == NULL )
  579.    {
  580.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  581.       return( FALSE );
  582.    }
  583.  
  584.    if ( string_array == (LPCTSTR *) NULL )
  585.    {
  586.       m_ErrorCode = ERROR_INVALID_HANDLE;
  587.       return( FALSE );
  588.    }
  589.  
  590.    if ( RegisterSource( log_name ) == TRUE )
  591.    {
  592.       if ( Report( eventError, 0, message_string_resource_identifier, number_of_strings, string_array ) != TRUE )
  593.       {
  594.          DeregisterSource();
  595.          return( FALSE );
  596.       }
  597.  
  598.       DeregisterSource();
  599.    }
  600.    else
  601.    {
  602.       return( FALSE );
  603.    }
  604.  
  605.    return( TRUE );
  606. }
  607.  
  608. void CEventLog::ReportError( LPCTSTR string_to_report )
  609. {
  610.    LPCTSTR string_array[ 1 ];
  611.  
  612.    if ( string_to_report == (LPCTSTR) NULL )
  613.    {
  614.       string_array[ 0 ] = "CEventLog::ReportError( NULL )";
  615.    }
  616.    else
  617.    {
  618.       string_array[ 0 ] = string_to_report;
  619.    }
  620.  
  621.    TRACE( "CEventLog::ReportError()\n" );
  622.    Report( eventError, 0, 0, 1, string_array );
  623. }
  624.  
  625. void CEventLog::ReportInformation( LPCTSTR string_to_report )
  626. {
  627.    LPCTSTR string_array[ 1 ];
  628.  
  629.    if ( string_to_report == (LPCTSTR) NULL )
  630.    {
  631.       string_array[ 0 ] = "CEventLog::ReportInformation( NULL )";
  632.    }
  633.    else
  634.    {
  635.       string_array[ 0 ] = string_to_report;
  636.    }
  637.  
  638.    Report( eventInformation, 0, 0, 1, string_array );
  639. }
  640.